﻿package com.gamemeal.particle.steering {
	import com.gamemeal.geom.VelocityPoint3;
	/**
	 * @author Colin
	 */
	public class Flee implements ISteering{
		private var imp:ISteering;
		public function Flee(enable3D:Boolean=true){
			if(enable3D){
				imp = new impFlee3D();
			}else{
				imp = new impFlee2D();
			}
		}
		public function apply(p:VelocityPoint3,tx:Number,ty:Number,tz:Number=null):void{
			imp.apply(p,tx,ty,tz);
		}
	}
}

import com.gamemeal.geom.VelocityPoint3;

class impFlee2D implements ISteering{
	public function apply(p:VelocityPoint3,tx:Number,ty:Number,tz:Number=null):void{
		tz;
		var dx:Number;
		var dy:Number;
		var dl:Number;
		
		//seek vector
		dx = -(tx - p.x);
		dy = -(ty - p.y);

		dl = Math.sqrt(dx*dx+dy*dy);

		var vl:Number = Math.sqrt(p.vx*p.vx+p.vy*p.vy);
		p.vx+=dx;
		p.vy+=dy;

		//set new speed as old speed;
		var scale:Number = vl/Math.sqrt(p.vx*p.vx+p.vy*p.vy);
		p.vx*=scale;
		p.vy*=scale;
	}
}

class impFlee3D implements ISteering{
	public function apply(p:VelocityPoint3,tx:Number,ty:Number,tz:Number=null):void{
		var dx:Number;
		var dy:Number;
		var dz:Number;
		var dl:Number;
		
		//seek vector
		dx = -(tx - p.x);
		dy = -(ty - p.y);
		dz = -(tz - p.z);

		dl = Math.sqrt(dx*dx+dy*dy+dz*dz);

		var vl:Number = Math.sqrt(p.vx*p.vx+p.vy*p.vy+p.vz*p.vz);
		p.vx+=dx;
		p.vy+=dy;
		p.vz+=dz;

		//set new speed as old speed;
		var scale:Number = vl/Math.sqrt(p.vx*p.vx+p.vy*p.vy+p.vz*p.vz);
		p.vx*=scale;
		p.vy*=scale;
		p.vz*=scale;
	}
}